package ningbo.file;

import java.io.*;
import java.lang.reflect.Method;

/**
 * @Author 宁铂
 * @Date 2021/7/18 21:12
 * @Version 1.0
 */
public class TestIn {
    /** 本类用于测试字节流的读取 */
    public static void main(String[] args) {
        method1(); //普通字节流
        method2(); //高效字节流
    }

    private static void method2() {
        BufferedInputStream bufferedInputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream("F:\\TestFile\\1.txt"));

           int b;
           while((b = bufferedInputStream.read()) != -1){
               System.out.println(b);
           }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                bufferedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void method1() {
        InputStream inputStream2 = null;
        try {
//            InputStream inputStream1 = new FileInputStream(new File("F:\\TestFile\\1.txt"));
            inputStream2 = new FileInputStream("F:\\TestFile\\1.txt");

            /** 使用流对象 read() 从输入流中读取一个数据字节(InputStream) */
            System.out.println(inputStream2.read());

            //循环读取文件内容直至读完
            int b;
            while ((b = inputStream2.read()) != -1){
                System.out.println(b);
            }
            //关闭流对象


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
